<?php
$sMsg="";
$Action="?mod=product.listcat.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if(isset($_GET['pid'])){ $pid = $_GET['pid'];}else{ $pid=0;}
if ($Method=="add")
{
	if(isset($_POST["c_ten"])) {$c_ten=addslashes($_POST["c_ten"]);}
	if(isset($_POST["c_id_cha"])) $c_id_cha=$_POST["c_id_cha"];
	if(isset($_POST["c_thutu"])) $c_thutu=$_POST["c_thutu"];
	if(isset($_POST["c_hienthi"])) $c_hienthi=$_POST["c_hienthi"];
	if (trim($c_ten)!="")
	{
			$sql="Insert into ".$prefix."loaisanpham(c_ten,c_id_cha,c_thutu,c_hienthi) values('$c_ten',$c_id_cha,$c_thutu,$c_hienthi)";
			//echo $sql;
			mysql_query($sql);
			Redirect("Đã thêm mới thành công","?mod=product.listcat");
	} 
}
if($Method=="edit")
{
	$id=$_GET["id"];
	$pid=$_GET["pid"];
	$sql="select * from ".$prefix."loaisanpham where c_id=$id";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$rows=mysql_fetch_array($result);
		$c_ten=stripslashes($rows["c_ten"]);
		$c_id_cha=$rows["c_id_cha"];
		$c_thutu=$rows["c_thutu"];
		$c_hienthi=$rows["c_hienthi"];
		$Submit="Cập nhập";
		$TitleTable="Sửa thông tin";
		$Action="?mod=product.listcat.add&method=update&pid=$pid&id=$id";
	}
}

if ($Method=="update")
{
	$Submit="Tạo Mới";
	$id=$_GET["id"];
	$pid=$_GET['pid'];
	if(isset($_POST["c_ten"])) {$c_ten=addslashes($_POST["c_ten"]);}
	if(isset($_POST["c_id_cha"])) $c_id_cha=$_POST["c_id_cha"];
	if(isset($_POST["c_thutu"])) $c_thutu=$_POST["c_thutu"];
	if(isset($_POST["c_hienthi"])) $c_hienthi=$_POST["c_hienthi"];
	if (trim($c_ten)!="")
	{
			$sql="update ".$prefix."loaisanpham set c_ten='$c_ten',c_id_cha=$c_id_cha,c_thutu=$c_thutu,c_hienthi=$c_hienthi where c_id=$id";
			//echo $sql;
			mysql_query($sql);
			Redirect("Cập nhập thành công","?mod=product.listcat");
	} 
}
?>
<script language="javascript1.2">
	function CheckFrm(_form)
	{
	if (_form.c_ten.value==""){alert("Bạn phải nhập tên danh mục");_form.c_ten.focus();return false;}
	if (_form.c_thutu.value==""){alert("Bạn phải nhập thứ tự sắp xếp");_form.c_thutu.focus();return false;}
	if (_form.c_hienthi.value==""){alert("Bạn phải chọn trạng thái");_form.c_hienthi.focus();return false;}
	}
</script>
	<?
	$sql = "select count(*) as total from ".$prefix."loaisanpham".$where."";
	$result = mysql_query($sql) or die("Can't connect database");
	$row = mysql_fetch_array($result);
	?>
<!--Bat dau form tao sua -->
<form name="frmHuyen" id="frmHuyen" action="<?=$Action;?>" method="post" onSubmit="return CheckFrm(this);">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right"><strong>
		  Tên danh mục :</strong></td>
	      <td width="497">
	      <input type="text" name="c_ten" value="<?=$c_ten;?>" size="40" class="TextBox"> *	      </td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Danh mục cha :</strong></td>
		<td width="497">
		<select name="c_id_cha" id="c_id_cha" style="width:120px">
		<option value="0" <? if($pid == 0){ ?> selected="selected" <? } ?>>Không có</option>
		<?
		$pSql = "select * from ".$prefix."loaisanpham";
		$rs= mysql_query($pSql);
		while($r = mysql_fetch_array($rs)){
		?>
		<option value="<?=$r['c_id']?>" <? if($pid == $r[c_id]){?> selected="selected" <? } ?>>
			<?=$r['c_ten']?>
		</option>
		<? }?>
		</select>
	
		</td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Thứ tự :</strong></td>
		<td width="497">
		<input type="text" name="c_thutu" value="<? if($c_thutu=="") { echo $row['total']+1;}else { echo $c_thutu;}?>" size="10" class="TextBox">*</td>
	  </tr>	  
	  <tr>
		<td width="254" align="right"><strong>Trạng thái :</strong></td>
		<td width="497">
		<select name="c_hienthi" id="c_hienthi" style="width:100px">
		<option value="1">Kích hoạt</option>
		<option value="0">Không kích hoạt</option>
		</select>
			<script language="javascript">
		 for(var i=0;i<document.frmHuyen.c_hienthi.length;i++)
		 {if(document.frmHuyen.c_hienthi[i].value=="<?=$c_hienthi?>")
		 {document.frmHuyen.c_hienthi.selectedIndex=i;break;}}
		</script>
		</td>
	  </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="Submit" value="<?=$Submit?>" class="Button" onClick="return CheckFrmTl();">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=product.listcat')"></td>
	  </tr>
	</table>
</form>
	